﻿using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace PartsRetrospect.Comm
{
    public class ConversionFun
    {
        public static string ObjToStrAndRplace(object obj)
        {
            if (obj == DBNull.Value) return "";
            if (obj == null) return "";
            string rst = obj.ToString();
            rst = rst.Replace('[', '<');
            rst = rst.Replace(']', '>');
            rst = rst.Replace('{', '【');
            rst = rst.Replace('}', '】');
            return rst;
        }

        public static string ObjToStr(object obj, string dfault = "")
        {
            if (obj == DBNull.Value) return dfault;
            if (obj == null) return dfault;
            string rts = obj.ToString();
            if (string.IsNullOrWhiteSpace(rts)) return dfault;
            return obj.ToString();
        }

        public static double ObjToDouble(object obj)
        {
            if (obj == DBNull.Value) return 0;
            if (obj == null) return 0;
            double rt = 0;
            try
            {
                rt = Convert.ToDouble(obj.ToString());
            }
            catch
            {
                rt = 0;
            }
            return rt;
        }


        public static int ObjToInt(object obj)
        {
            if (obj == DBNull.Value) return 0;
            if (obj == null) return 0;
            int rt = 0;
            try
            {
                rt = Convert.ToInt32(obj.ToString());
            }
            catch
            {
                rt = -1;
            }
            return rt;
        }

        public static int ObjToInt(object obj, int def = -1)
        {
            if (obj == DBNull.Value) return 0;
            if (obj == null) return 0;
            int rt = 0;
            try
            {
                rt = Convert.ToInt32(obj.ToString());
            }
            catch
            {
                rt = def;
            }
            return rt;
        }

        public static decimal ObjToDecimal(object obj)
        {
            if (obj == DBNull.Value) return 0;
            if (obj == null) return 0;
            decimal rt = 0;
            try
            {
                rt = Convert.ToDecimal(obj.ToString());
            }
            catch
            {
                rt = 0;
            }
            return rt;
        }
        public static DataTable frmToTable(string tableName, string htmls, bool removeSpace = false)
        {
            DataTable dt = new DataTable(tableName);
            dt.Rows.Add();
            string[] fcls = htmls.Split('&');
            for (int j = 0; j < fcls.Length; j++)
            {
                string[] sr = fcls[j].Split('=');
                if (sr[0].StartsWith("_")) continue;
                if (removeSpace && string.IsNullOrWhiteSpace(sr[1])) continue;
                dt.Columns.Add(sr[0]);
                dt.Rows[0][sr[0]] = sr[1];

            }
            return dt;
        }


        public static DataTable dtRowFilter(DataTable ydt, string RowFilterStr)
        {
            ydt.DefaultView.RowFilter = null;
            ydt.DefaultView.RowFilter = RowFilterStr;
            DataTable szdt = ydt.DefaultView.ToTable();
            ydt.DefaultView.RowFilter = null;
            return szdt;

        }
        public static void WriteTxt(string txt)
        {

            string stmp = System.Reflection.Assembly.GetExecutingAssembly().Location;
            stmp = stmp.Substring(0, stmp.LastIndexOf('\\'));//删除文件名
            StreamWriter sw = null;
            try
            {
                txt = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + ":    " + txt + "\r\n--------------------------------------------------------------";
                sw = File.AppendText(stmp + "\\log\\" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
                sw.WriteLine(txt, Encoding.UTF8);
                sw.Flush(); sw.Close();
            }
            catch (System.IO.IOException ex)
            {
                if (sw != null) sw.Close();
            }
            finally
            {
                if (sw != null) sw.Close();
            }

        }

        public static string strJM(string str)
        {
            if (string.IsNullOrWhiteSpace(str)) return "";
            byte[] by = new byte[str.Length];
            by = System.Text.Encoding.UTF8.GetBytes(str);
            string r = Convert.ToBase64String(by);
            return r;
        }

        public static string strDJM(object obj)
        {
            string r = "";
            if (obj is Byte[])
            {
                byte[] b = (byte[])obj;
                string rs = Encoding.UTF8.GetString(b);

                byte[] by = Convert.FromBase64String(rs);
                r = Encoding.UTF8.GetString(by);
            }
            else
            {
                byte[] by = Convert.FromBase64String(obj.ToString());
                r = Encoding.UTF8.GetString(by);

            }
            return r;
        }
    }
}
